package com.zwt.passbook.vo;

import com.zwt.passbook.constant.ErrorCode;
import com.zwt.passbook.dao.MerchantsDao;
import com.zwt.passbook.entity.Merchants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

/**
 * <h2>创建商户请求对象</h2>
 * Created by zhangchengfa on 2019-03-22 14:28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateMerchantsRequest {

    /** 商户名称 */
    private String name;

    /** 商户 logo */
    private String logoUrl;

    /** 商户营业执照 */
    private String businessLicenseUrl;

    /** 商户联系电话 */
    private String phone;

    /** 商户地址 */
    private String address;

    /**
     * <h2>验证请求的有效性</h2>
     * @param merchantsDao {@link MerchantsDao}
     * @return {@link ErrorCode}
     */
    public ErrorCode validate(MerchantsDao merchantsDao) {

        if (StringUtils.isEmpty(this.name)){
            return ErrorCode.EMPTY_NAME;
        }
        if (merchantsDao.findByName(this.name) != null){
            return ErrorCode.DUPLICATE_NAME;
        }
        if (StringUtils.isEmpty(this.logoUrl)){
            return ErrorCode.EMPTY_LOGO;
        }
        if (StringUtils.isEmpty(this.address)){
            return ErrorCode.EMPTY_ADDRESS;
        }
        if (StringUtils.isEmpty(phone)){
            return ErrorCode.ERROR_PHONE;
        }

        return ErrorCode.SUCCESS;
    }

    /**
     * <h2>将请求对象转化为商户对象</h2>
     * @return {@link Merchants}
     */
    public Merchants toMerchants() {

        Merchants merchants = new Merchants();
        merchants.setName(name);
        merchants.setLogoUrl(logoUrl);
        merchants.setBusinessLicenseUrl(businessLicenseUrl);
        merchants.setPhone(phone);
        merchants.setAddress(address);

        return merchants;
    }
}
